﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sustainalytics.ClientDW.Synchronization;
using  Sustainalytics.Synchronization.Interface;

namespace PerformanceTester
{
    class Program
    {
        static void Main(string[] args)
        {
            //var mongoCnx = "mongodb://localhost/UnitTests";
            //var sqlCnx = "Data Source=sust-test-app-srv.cloudapp.net,51433;User ID=sql_server_admin;Password=G4nd4lf2014;Initial Catalog=DSRSystem;Pooling=True;Min Pool Size=10;Connect Timeout=10";
          
            //var step = new CorporateDataSyncStep3();
            //var
            //    sw = Stopwatch.StartNew();
            //try
            //{
            //    step.SqlCorporateData2Mongo(mongoCnx, sqlCnx, DateTime.MinValue, 1000, 9);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //}
            //System.GC.Collect();
            //System.Threading.Thread.Sleep(2000);
            //Console.WriteLine("Duration:  {0}", sw.ElapsedMilliseconds);


            HadoopTest();

            //   Sql2Mongo();
        }

        private static void HadoopTest()
        {
            Console.WriteLine("press key");
            
            Console.ReadKey();
           //var mongoCnx = "mongodb://localhost/ClientDW_B";
            var mongoCnx = "mongodb://sustadmin:G4nd4lf2014@10.0.1.10:27017,10.0.1.11:27017/ClientDW_B?connect=replicaset;replicaSet=gapStage";// 

        
            //var sqlCnx =  "Data Source=sust-test-app-srv.cloudapp.net,51433;User ID=sql_server_admin;Password=G4nd4lf2014;Initial Catalog=DSRSystem;Pooling=True;Min Pool Size=10;Connect Timeout=10";
            //var to = DateTime.Now;
            //var from = to.AddDays(-1);
            Console.WriteLine("Start");


            {
                var connStr = new CorporateDataConnectionString()
                {
                    HbaseUrl = "https://cddb.azurehdinsight.net",
                    Password = "Cddbase2015!",
                    UserName = "admin"
                };



                var result = (new HadoopCorporateData2Mongo(mongoCnx, connStr, DateTime.MinValue, DateTime.UtcNow, 1000, 8,  (s) => Console.WriteLine(s) )).Run();

                Console.WriteLine("Stop" + result.Count());
            }

            Console.ReadKey();
        }

        private static void Sql2Mongo()
        {
            var step = new CorporateDataSyncStep3();

            var mongoCnx = "mongodb://localhost/UnitTests";
            var sqlCnx =
                "Data Source=sust-test-app-srv.cloudapp.net,51433;User ID=sql_server_admin;Password=G4nd4lf2014;Initial Catalog=DSRSystem;Pooling=True;Min Pool Size=10;Connect Timeout=10";
            var to = DateTime.Now;
            var from = to.AddDays(-1);


            Console.ReadKey();


            //var sw = Stopwatch.StartNew();
            //try
            //{
            //    step.SqlCorporateDataMongo(mongoCnx, sqlCnx, DateTime.MinValue, 1000, 1);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //}

            //sw.Stop();
            //Console.WriteLine("Duration:  {0}", sw.ElapsedMilliseconds);

            //System.Threading.Thread.Sleep(2000);

            //sw = Stopwatch.StartNew();
            //try
            //{
            //    step.SqlCorporateDataMongo(mongoCnx, sqlCnx, DateTime.MinValue, 2500, 1);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //}

            //sw.Stop();
            //Console.WriteLine("Duration:  {0}", sw.ElapsedMilliseconds);


            //System.Threading.Thread.Sleep(2000);


            //sw = Stopwatch.StartNew();
            //try
            //{
            //    step.SqlCorporateDataMongo(mongoCnx, sqlCnx, DateTime.MinValue, 5000, 1);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //}
            //sw.Stop();
            //Console.WriteLine("Duration:  {0}", sw.ElapsedMilliseconds);


            //System.Threading.Thread.Sleep(2000);


            //sw = Stopwatch.StartNew();
            //try
            //{
            //    step.SqlCorporateDataMongo(mongoCnx, sqlCnx, DateTime.MinValue, 10000, 1);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //}
            //sw.Stop();
            //Console.WriteLine("Duration:  {0}", sw.ElapsedMilliseconds);

            var
                sw = Stopwatch.StartNew();
            try
            {
                step.SqlCorporateData2Mongo(mongoCnx, sqlCnx, DateTime.MinValue, 1000, 9);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            System.GC.Collect();
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("Duration:  {0}", sw.ElapsedMilliseconds);


            sw = Stopwatch.StartNew();
            try
            {
                step.SqlCorporateData2Mongo(mongoCnx, sqlCnx, DateTime.MinValue, 1000, 16);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            sw.Stop();
            Console.WriteLine("Duration:  {0}", sw.ElapsedMilliseconds);

            System.GC.Collect();
            System.Threading.Thread.Sleep(2000);

            sw = Stopwatch.StartNew();
            try
            {
                step.SqlCorporateData2Mongo(mongoCnx, sqlCnx, DateTime.MinValue, 2000, 16);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            sw.Stop();
            Console.WriteLine("Duration:  {0}", sw.ElapsedMilliseconds);

            System.GC.Collect();
            System.Threading.Thread.Sleep(2000);


            sw = Stopwatch.StartNew();
            try
            {
                step.SqlCorporateData2Mongo(mongoCnx, sqlCnx, DateTime.MinValue, 4000, 16);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            sw.Stop();
            Console.WriteLine("Duration:  {0}", sw.ElapsedMilliseconds);


            System.GC.Collect();
            System.Threading.Thread.Sleep(2000);


            sw = Stopwatch.StartNew();
            try
            {
                step.SqlCorporateData2Mongo(mongoCnx, sqlCnx, DateTime.MinValue, 6000, 9);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            sw.Stop();
            Console.WriteLine("Duration:  {0}", sw.ElapsedMilliseconds);
            System.GC.Collect();
            System.Threading.Thread.Sleep(2000);

            Console.ReadLine();
        }
    }
}


//var dic = new ConcurrentDictionary<Guid, int[]>();

//Parallel.For(0, 100000, i => dic.AddOrUpdate(Guid.NewGuid(), new int[50], (x,y) => new int[50]));


//Console.WriteLine("gata");
//Console.ReadKey();
//return;